<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\ImageModel;
use Intervention\Image\Image;

class ImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //首页
        $imageModel = new ImageModel;
        $images = $imageModel->getImages();

        return view('admin.image.index')->with('images' , $images);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //增加
        return view('admin.image.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //保存
        $data = $request->except('_token');
        $imageModel = new ImageModel;
        $res = $imageModel->insertImage($data);
        
        if ($res) {
            return ['msg' => '添加成功' , 'status' => 'success'];
        } else { 
            return ['msg' => '添加失败' , 'status' => 'fail'];
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //修改
        $imageModel = new ImageModel;
        $image = $imageModel->getImage($id);
    
        return view('admin.image.edit')->with('image' , $image);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //更新
        $data = $request->except('_token' , '_method');
        $imageModel = new ImageModel;
        $res = $imageModel->upImages($id , $data);

        if ( $res ) {
            return ['msg' => '更新成功' , 'status' => 'success'];
        } else {
            return ['msg' => '更新失败' , 'status' => 'fail'];
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        $imageModel = new ImageModel;
        $res = $imageModel->delImage($id);

        if ( $res ) {
            return ['msg' => '删除成功' , 'status' => 'success'];
        } else {
            return ['msg' => '删除失败' , 'status' => 'fail'];
        }
        
    }

    //图片上传
    public function upload(Request $request) {
        //获取图片信息
        $image = $request->file('file');

        if ($image->isValid()) {
            //获取图片扩展
            $ext = $image->getClientOriginalExtension();
            //获取图片大小
            $size = $image->getClientSize();

            $allow_ext = ['png','jpeg','png','jpg','bmp'];
            $allow_size = 1024*1024*4;

            //后台验证
            if ( !in_array($ext , $allow_ext) || $size > $allow_size) {
                return 'fail';
            }

            //路径
            $img_path = 'uploads/'.date('Y-m-d');
            //名称
            $img_name = time().'.'.$ext;
            //开始上传
            if ( $image->move($img_path,$img_name) ) {
                $url = $img_path.'/'.$img_name;
                return ['status' => 'success' , 'url' => $url];
            }

            return 'fail';
        }
    }
}
